package com.osc.exam.controller;

import com.osc.exam.constant.GlobalConstant;
import com.osc.exam.entity.AdminResponse;
import com.osc.exam.entity.domain.Post;
import com.osc.exam.service.PostService;
import com.osc.exam.service.ProgressScaleService;
import com.osc.exam.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/scale")
@Api("进度比例统计")
public class ProgressScaleController {

    @Autowired
    private ProgressScaleService progressScaleService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private PostService postService;

    /**
     * 人数及比例信息
     * @return
     */
    @GetMapping("/data")
    @ApiOperation(value = "人数及比例信息--赵琳", notes = "人数及比例信息")
    @RequiresPermissions("Retrieve")
    public ResponseEntity scaleData(){
        Map<String,Object> res = new HashMap<>();
        res = progressScaleService.getInterDate();
        int resume = studentService.getResume();
        res.put("renum",resume);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS,"查询统计信息成功!",res));
    }

    /**
     * 以岗位划分
     * @return
     */
    @GetMapping("/post-scale")
    @ApiOperation(value = "以岗位划分--赵琳", notes = "以学校纬度划分")
    @RequiresPermissions("Retrieve")
    public ResponseEntity postLatitude(){
        List<Post> posts = postService.queryAll();
        List<Object> res = new ArrayList<>();
        List<Map<String,Object>> num =  studentService.getPost();
        List<Long> pnum = new ArrayList();
        List<String> p = new ArrayList();
        for(Post post:posts){
            boolean flag = true;
            p.add(post.getPostName());
            for(Map m : num){
                if(m.get("post").equals(post.getPostName())){
                    System.out.println(m.get("post"));
                    pnum.add((Long) m.get("num"));
                    System.out.println(m.get("num"));
                    flag = false;
                    break;
                }
            }
            if(flag){
                pnum.add((long) 0);
            }
        }
        System.out.println(pnum);
        res.add(pnum);
        res.add(p);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS,"岗位划分成功!",res));
    }

    /**
     * 以学校纬度划分
     * @return
     */
    @GetMapping("/school")
    @ApiOperation(value = "以学校纬度划分--赵琳", notes = "以学校纬度划分")
    @RequiresPermissions("Retrieve")
    public ResponseEntity schoolLatitude(){
        List<Map<String,Object>> num =  studentService.selectPartStu();
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS,"学校维度划分成功!",num));
    }

    /**
     * 查询所有招聘岗位
     * @return
     */
    @GetMapping("/post")
    @ApiOperation(value = "查询所有招聘岗位--赵琳", notes = "查询所有招聘岗位")
    public ResponseEntity getAllPost(){
        try {
            List<Post> posts = postService.queryAll();
            return ResponseEntity.status(HttpStatus.OK)
                    .body(AdminResponse.result(GlobalConstant.SUCCESS, "查询成功!",posts));
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.OK)
                    .body(AdminResponse.result(GlobalConstant.FAIL, e.getMessage()));
        }
    }
}
